<template>
    <div class="order">
		<!-- 公共标题 -->
		<div class="common-tel">
			收货人信息
			<div class="common-tel-dot">
				<em></em>
				<em></em>
				<em></em>
			</div>
		</div>
		<!-- 收货人信息 -->

		<div class="common-form" v-if=" add.status != 400 ">
			<div class="order-info">
				<a  class="order-info-a" @click="switchTo('/AddressList')"></a>
				<div class="order-info-name">
					{{add.name}}  {{add.tel}}
				</div>
				<div class="order-info-add">
					<label>地址：</label>
					<span>{{add.province}}{{add.city}}{{add.area}}{{add.address}}</span>
				</div>
			</div>
		</div>
		<div class="common-form" v-else>
			<!-- <router-link :to="'/AddressAdd'" > -->
			<div class="order-info-none" @click="switchTo('/orderAdd')"><em>+</em>添加收货地址</div>
			<!-- </router-link>  -->
		</div>
		<!-- 公共标题 -->
		<div class="common-tel">
			商品信息
			<div class="common-tel-dot">
				<em></em>
				<em></em>
				<em></em>
			</div>
		</div>
		<!-- 商品信息 -->
		<div class="common-form" >
			<div class="order-shop">
				<p class="order-shop-tel"><i></i>商品信息</p>
				<div class="order-shop-info">
					<img :src="url + detail.thumbnail">
					<div>
						<p>{{detail.goods_title}}</p>
						<p>{{detail.spec_2}} {{detail.spec_1}}</p>
						<p>
							<a v-if="vip == 0">￥{{detail.total_fee}}</a>
							<a v-else>￥{{detail.total_fee}}</a>
						</p>
					</div>
				</div>
				<div class="order-shop-more">购买数量 <em>{{quantity}}</em></div>
				<div class="order-shop-more">运费 <em>{{price}}</em></div>
				<div class="order-shop-more">商品金额 <em>￥{{detail.total_fee}}</em></div>
				<div class="order-shop-more">买家留言 <input type="text" placeholder="选填" /></div>
			</div>
		</div>
		<!-- 公共标题 -->
		<!-- <div class="common-tel">
			支付方式
			<div class="common-tel-dot">
				<em></em>
				<em></em>
				<em></em>
			</div>
		</div> -->
		<!-- 支付方式 -->
		<!-- <div class="common-form">
			<div class="order-pay">
				<img src="build/img/1.jpg" alt="" />
				<div>
					<p>微信支付</p>
					<p>微信支付  更快更安全</p>
				</div>
				<em></em>
			</div>
			<div class="order-pay">
				<img src="build/img/2.jpg" alt="" />
				<div>
					<p>微信支付</p>
					<p>微信支付  更快更安全</p>
				</div>
				<em class="order-pay-pink"></em>
			</div>
		</div> -->
		<div class="footer-100"></div>
		<!-- 支付 -->
		<div class="order-footer">
			<div>
				实付款：<span>￥<em>{{Number(price)+Number(detail.total_fee)}}</em></span>
			</div>
			<a @click="pay">立即支付</a>
		</div>
	</div>
</template>

<script>
import axios from "axios";
import qs from 'qs'
import url from 'url'
import wx from 'weixin-jsapi'
    export default {
		name:'seckillOrder',
		data () {
            return {
				add:{},
				firm:[],
				url:'http://api.huiwanzhong365.com',
				detail:{},
				total_price:'',
				price:'',
				vip:'',
				quantity:1
            }
		},
		methods:{
			switchTo(path){
				// console.log(this.$router)
				this.$router.push(path)
			},    
			//获取收货地址
			getAdd:function(){
				var that = this
				var openid = localStorage.getItem("openid")
				console.log(openid)
                var upload = {
                    openid:openid,
                }
                this.$http.post('/vipmember/index/getDefaultAddress',qs.stringify(upload)).then(res=>{
					console.log(res.data)
					this.add = res.data
					this.yunfei(res.data.province)
                }).catch(err=>{
                    console.log(err)
                })
			},
			//查看运费
			yunfei:function(province){
				var that = this
				 
				var openid = localStorage.getItem("openid")
				console.log(openid)
                var upload = {
					openid:openid,
					province:province,
					goods_id:this.$route.query.id
                }
                this.$http.post('/index/merchant/goods/query_freight',qs.stringify(upload)).then(res=>{
					console.log(res.data)
					// res.data.price = res.data.price.split('.')[0]
					this.price = res.data.price
					// this.total_price = Number(this.price + this.detail.total_fee)
					// console.log(Number(that.price))
					// console.log(this.detail.total_fee)
					//this.add = rs.data
                }).catch(err=>{
                    console.log(err)
                })
			},
			
			// 确认订单信息
			confirm:function(){
				var that = this
				var openid = localStorage.getItem("openid")
				var upload = {
					openid:openid,
					// goods_id:9,
					// attr_id:2
					goods_id:this.$route.query.id,
					attr_id:this.$route.query.s,
					quantity:this.$route.query.quantity
				}
				this.$http.post('/index/merchant/goods/seckillaffirm',qs.stringify(upload)).then(res=>{
					console.log(res.data)
					this.detail = res.data
                }).catch(err=>{
                    console.log(err)
                })
				
			},
			// confirm:function(){
			// 	//console.log(this.$route.params.id)
			// 	var that = this
			// 	var openid = 'o1KIj095lVCrkl8pE4S1wXgbcicQ'
            //     var upload = {
			// 		openid:openid,
			// 		goods_id:9,
			// 		attr_id:2
            //     }
            //     this.$http.post('/index/merchant/goods/seckillaffirm',qs.stringify(upload)).then(res=>{
			// 		console.log(res.data)
			// 		this.detail = res.data
            //     }).catch(err=>{
            //         console.log(err)
            //     })
			// },
			pay:function(){
				var that = this
				// console.log(JSON.stringify(data))
				// console.log()
				// if(this.add.status == 400){
				// 	layer.msg(this.add.info);
				// }else{
					var upload = {
                        openid:localStorage.getItem("openid"),
						goods_id:this.$route.query.id,
						attr_id:this.$route.query.s,
						addressee:this.add.name,
						number:this.add.tel,
						province:this.add.province,
						city:this.add.city,
						area:this.add.area,
						address:this.add.address
					}
					this.$http.post('/index/merchant/goods/seckillOrder',qs.stringify(upload)).then(res=>{
						console.log(res.data)
						if(res.data.status == 400){
							// layer.msg(res.data.info);
							layer.open({
								content: res.data.info
								,btn: ['成为会员', '返回']
								,yes: function(index){
									// location.reload();
									
									layer.close(index);
									// this.$router.push({name:'openVip'});
									that.$router.push({path:'/openVip'});
							
								}
							});
						}else{
							wx.chooseWXPay({
								// appId:res.data.appId,
								timestamp:res.data.timeStamp, // 支付签名时间戳，注意微信jssdk中的所有使用timestamp字段均为小写。但最新版的支付后台生成签名使用的timeStamp字段名需大写其中的S字符
								nonceStr: res.data.nonceStr, // 支付签名随机串，不长于 32 位
								package:res.data.package, // 统一支付接口返回的prepay_id参数值，提交格式如：prepay_id=\*\*\*）
								signType: 'MD5', // 签名方式，默认为'SHA1'，使用新版支付需传入'MD5'
								paySign: res.data.paySign, // 支付签名
								success: function (res) {
									// 支付成功后的回调函数
									console.log('pay success');
									// 
									that.$router.push({name:'SpikeOrderList'});
								},
								error: function (res) {
									// 支付失败的回调函数
									console.log(res);
									//alert(res.errMsg);
								}
							});
						}
						
					}).catch(err=>{
						console.log(err)
					})
				// }
				
			},
		},
		mounted(){
			this.getAdd();
			this.confirm();
			// this.yunfei();
			this.quantity = this.$route.query.quantity
		}
    }
</script>

<style scoped>

</style>